package org.suren.littlebird;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.internal.PerspectiveBarContributionItem;
import org.eclipse.ui.internal.PerspectiveBarManager;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * @author suren
 * @since 2015-2-4
 */
@SuppressWarnings("restriction")
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "org.suren.littlebird"; //$NON-NLS-1$

	public static final String S_PERS_ID = "org.suren.littlebird.perspective.SuRenPerspectiveFactory";

	// The shared instance
	private static Activator plugin;

	/**
	 * The constructor
	 */
	public Activator() {
	}

	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;

		tryAddPerspective();
	}

	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	/**
	 * 在eclipse.buildId=4.3.0.I20130605-2000中没有getPerspectiveBar这个方法
	 */
	private void tryAddPerspective()
	{
		try
		{
			IWorkbench workbench = this.getWorkbench();
			IWorkbenchWindow activeWorkbenchWin = workbench.getActiveWorkbenchWindow();
			if(activeWorkbenchWin != null)
			{
//				PerspectiveBarManager barManager = ((WorkbenchWindow)activeWorkbenchWin).getPerspectiveBar();
//
//				if(barManager.find(S_PERS_ID) == null)
//				{
//					IPerspectiveDescriptor perspective = workbench.getPerspectiveRegistry().findPerspectiveWithId(S_PERS_ID);
//					PerspectiveBarContributionItem item = new PerspectiveBarContributionItem(perspective, activeWorkbenchWin.getActivePage());
//
//					barManager.add(item);
//				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
